package cn.bluethink.eguan.core.entity;

import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;

import cn.bluethink.eguan.model.core.EgBag;
import cn.bluethink.eguan.model.core.EgCheckStatus;
import cn.bluethink.eguan.model.core.EgGoods;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;

/**
 * 商品实体模型
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.wanglei于2018/11/26设计并构建初始版本v0.3.0
 */
@Table(name = "eg_goods")
public class EgGoodsEntity {

    // 主键ID
    @Id
    private Long gdid;

    // 商品类型
    private Integer otype;

    // 商品标题
    private String name;

    // 商品图标
    private String icon;

    // 商品价格
    private Integer price;

    // 商品数量
    private Integer number;
    
    // 商品剩余数量
    private Integer remain;

    // 商品创建者
    private Long uid;

    // 商品创建时间
    private Date ctime;

    // 商品更新时间
    private Date utime;

    // 包裹ID
    private Long bagid;

    // 商品所属者的类型
    private Integer ownerOtype;

    // 商品所属者
    private Long ownerOid;

    private Integer checkStatus;

    public EgGoodsEntity() {

    }

    /**
     * 创建实体模型
     * 
     * @param goods
     * @param bagid
     */
    public EgGoodsEntity(EgGoods goods, Long uid) {
        this.gdid = goods.getId();
        this.otype = goods.getType() == null ? EgOType.GOODS : goods.getType().getOtid();
        this.name = goods.getName() == null ? "" : goods.getName();
        this.icon = goods.getIcon() == null ? "" : goods.getIcon();
        this.price = goods.getPrice() == null ? 0 : goods.getPrice();
        this.number = goods.getNumber() == null ? 1 : goods.getNumber();
        this.remain = goods.getRemain() == null ? 0 : goods.getRemain();
        this.uid = uid;
        this.bagid = goods.getBag() == null ? null : goods.getBag().getId();
        this.ownerOtype = goods.getOwner().getType().getOtid();
        this.ownerOid = goods.getOwner().getId();
        this.checkStatus = goods.getCheckStatus() == null ? EgCheckStatus.STATUS_PENDING : goods.getCheckStatus();
        this.ctime = new Date();
        this.utime = ctime;
    }

    /**
     * 更新实体模型
     * 
     * @param goods
     */
    public EgGoodsEntity(EgGoods goods) {
        this.gdid = goods.getId();
        this.name = goods.getName() == null ? null : goods.getName();
        this.icon = goods.getIcon() == null ? null : goods.getIcon();
        this.price = goods.getPrice() == null ? null : goods.getPrice();
        this.number = goods.getNumber() == null ? null : goods.getNumber();
        this.remain = goods.getRemain() == null ? null : goods.getRemain();
        if (goods.getOwner() == null) {
            this.ownerOtype = null;
            this.ownerOid = null;
        }
        this.bagid = goods.getBag() == null ? null : goods.getBag().getId();
        this.ownerOtype = goods.getOwner().getType() == null ? null : goods.getOwner().getType().getOtid();
        this.ownerOid = goods.getOwner().getId();
        this.utime = new Date();
    }

    /**
     * 转为业务模型
     * 
     * @return
     */
    public EgGoods createEgGoods() {
        EgGoods goods = new EgGoods();
        goods.setId(gdid);
        goods.setName(name);
        goods.setIcon(icon);
        goods.setPrice(price);
        goods.setNumber(number);
        goods.setRemain(remain);
        if (bagid != null && bagid > 0) {
            goods.setBag(new EgBag(bagid, null));
        } else {
            goods.setBag(null);
        }
        if (ownerOtype != null && ownerOid != null && ownerOtype > 0 && ownerOid > 0) {
            goods.setOwner(new EgObject(ownerOid, null, new EgOType(ownerOtype, null)));
        } else {
            goods.setOwner(null);
        }
        goods.setRemain(remain);
        goods.setUtime(utime);
        goods.setCheckStatus(checkStatus);
        return goods;
    }

    public Long getGdid() {
        return gdid;
    }

    public void setGdid(Long gdid) {
        this.gdid = gdid;
    }

    public Integer getOtype() {
        return otype;
    }

    public void setOtype(Integer otype) {
        this.otype = otype;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getRemain() {
        return remain;
    }

    public void setRemain(Integer remain) {
        this.remain = remain;
    }

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }

    public Date getUtime() {
        return utime;
    }

    public void setUtime(Date utime) {
        this.utime = utime;
    }

    public Long getBagid() {
        return bagid;
    }

    public void setBagid(Long bagid) {
        this.bagid = bagid;
    }

    public Integer getOwnerOtype() {
        return ownerOtype;
    }

    public void setOwnerOtype(Integer ownerOtype) {
        this.ownerOtype = ownerOtype;
    }

    public Long getOwnerOid() {
        return ownerOid;
    }

    public void setOwnerOid(Long ownerOid) {
        this.ownerOid = ownerOid;
    }

    public Integer getCheckStatus() {
        return checkStatus;
    }

    public void setCheckStatus(Integer checkStatus) {
        this.checkStatus = checkStatus;
    }

}
